package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.ClasslibMapper;
import com.gxa.zhonglong.mapper.GoodsMapper;
import com.gxa.zhonglong.mapper.HeadquartersMapper;
import com.gxa.zhonglong.mapper.StoreBarterMapper;
import com.gxa.zhonglong.pojo.Goods;
import com.gxa.zhonglong.pojo.StoreBarter;
import com.gxa.zhonglong.service.StoreBarterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wanjun
 * @version 1.0
 * @date 2021/10/27 14:44
 */
@Transactional(rollbackFor = Throwable.class)
@Service
public class StoreBarterServiceImpl implements StoreBarterService {
    @Autowired
    StoreBarterMapper storeBarterMapper=null;
    @Autowired
    ClasslibMapper classlibMapper=null;
    @Autowired
    HeadquartersMapper headquartersMapper=null;

    //多商品编码查商品详情
    @Override
    public Map<String,Object> selectGoodByCode(String goodCoding) {
        Map<String, Object> map = storeBarterMapper.selectGoodByCode(goodCoding);
        return map;
    }
    @Override
    public void add(StoreBarter storeBarter,String storeBarterNum, List<String> goodsCoding) {
         storeBarterMapper.add(storeBarter);
         storeBarterMapper.addCo(storeBarterNum,goodsCoding);
    }

    //商品编号查调拨单编号
    @Override
    public String selectNumByGoodCoding(String goodCoding) {
        String s = storeBarterMapper.selectNumByGoodCoding(goodCoding);
        if(s!=null){
            //商品调拨单中存在某个商品,而且是未审核状态,返回字符串,否则返回空(不用理会)
            Integer integer = storeBarterMapper.selectStateByNum(s);
            System.out.println(integer);
            if(integer!=null&&integer==0){
                return s;
            }
        }
        return null;
    }

    @Override
    public List<StoreBarter> selectAll(Integer offset, Integer size) {
        List<StoreBarter> storeBarters = storeBarterMapper.selectAll((offset - 1) * size, size);
        return storeBarters;
    }

    @Override
    public List<StoreBarter> selectAll2() {
        List<StoreBarter> storeBarters2 = storeBarterMapper.selectAll2();
        return storeBarters2;
    }

    @Override
    public List<StoreBarter> selectByField(Timestamp from,  Timestamp to,
                                           String storeBarterGclass,  String storeBarterNews,
                                           String storeBarterNewc,  String storeBarterOlds,
                                           String storeBarterOldc,  String storeBarterCusername,
                                           Integer offset,  Integer size) {
        List<StoreBarter> storeBarters = storeBarterMapper.selectByField(from, to, storeBarterGclass, storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername, (offset-1)*size, size);
        return storeBarters;
    }

    @Override
    public List<StoreBarter> selectByField2(Timestamp from, Timestamp to, String storeBarterGclass, String storeBarterNews, String storeBarterNewc, String storeBarterOlds, String storeBarterOldc, String storeBarterCusername) {
        List<StoreBarter> storeBarters2 = storeBarterMapper.selectByField2(from, to, storeBarterGclass, storeBarterNews, storeBarterNewc, storeBarterOlds, storeBarterOldc, storeBarterCusername);
        return storeBarters2;
    }

    @Override
    public void updateState0(String storeBarterNum) {
        storeBarterMapper.updateState0(storeBarterNum);
    }

    @Override
    public void updateState2(String storeBarterNum) {
        storeBarterMapper.updateState2(storeBarterNum);
    }

    @Override
    public void delete(String storeBarterNum) {
         storeBarterMapper.delete(storeBarterNum);
         storeBarterMapper.deleteCo(storeBarterNum);
    }

    @Override
    public void updateState1(String storeBarterNum,String storeBarterEusername,Timestamp storeBarterEtime) {
        storeBarterMapper.updateState1(storeBarterNum);
        //通过调拨单编码查询需要调到那个门店
        String selectBarterS = storeBarterMapper.selectBarterS(storeBarterNum);
        String selectBarterC = storeBarterMapper.selectBarterC(storeBarterNum);
        //调拨单编码找到很多商品
        List<String> goodsCoding = storeBarterMapper.selectGoodsCodingByNum(storeBarterNum);
        System.out.println("啊啊啊:"+goodsCoding);
        //将门店信息写入商品中
        storeBarterMapper.updateGoodsSC(selectBarterS,selectBarterC,goodsCoding);
        //审批单上边添加审批时间和审批人
        storeBarterMapper.updateBarterD(storeBarterNum, storeBarterEusername, storeBarterEtime);
    }

    //修改调拨单
    @Override
    public void updateBarter(String storeBarterNum, Timestamp storeBarterCtime, String storeBarterNews, String storeBarterNewc) {
        storeBarterMapper.updateBarter(storeBarterNum, storeBarterCtime, storeBarterNews, storeBarterNewc);
    }

    //查询全部门店
    @Override
    public List<String> selectAllStore() {
        List<Map<String, Object>> list = storeBarterMapper.selectAllStore();
        List<String> stringList=new ArrayList<>();
        for (Map<String, Object> map : list) {
            stringList.add((String)map.get("subbranch_name"));
        }
        return stringList;
    }

    //查询门店下柜台
    @Override
    public List<String> selectCounter(String barterName) throws Exception{
        List<Map<String, Object>> list = storeBarterMapper.selectAllStore();
        System.out.println("所有分店信息"+list);
        for (Map<String, Object> map : list) {
            System.out.println("map是:"+map);
            if(map.get("subbranch_name").equals(barterName)){
                System.out.println("分店的id:"+map.get("subbranch_id"));
                List<String> stringList = storeBarterMapper.selectCounter(((Long) map.get("subbranch_id")));
                System.out.println("门店下柜台"+stringList);
                return stringList;
            }
        }
        throw  new BusinessException(ExceptionCode.SERVER);
    }

    //删除调拨表中的商品
    @Override
    public void deleteBagood(String storeBarterNum, String goodCoding) {
     storeBarterMapper.deleteBagood(storeBarterNum, goodCoding);
    }

    @Override
    public List<Map<String, Object>> selectBaDeta(String storeBarterNum, Integer offset, Integer size) {
        List<Map<String, Object>> list = storeBarterMapper.selectBaDeta(storeBarterNum, (offset-1)*size, size);
        return list;
    }

    @Override
    public List<Map<String, Object>> selectBaDeta1(String storeBarterNum) {
        List<Map<String, Object>> list1 = storeBarterMapper.selectBaDeta1(storeBarterNum);
        return list1;
    }


}
